package com.htc.adapter;

import java.io.File;
import java.util.ArrayList;

import com.htc.constants.Constants;
import com.htc.dlc.R;
import com.htc.dlc.SettingsActivity;
import com.htc.model.dto.NewsModuleDTO;
import com.htc.webservice.PicassoUtils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NewsAdapter extends ArrayAdapter<Object> {

	Context mContext;
	private ArrayList<Object> arrObject;

	public NewsAdapter(Context context, int resource, ArrayList<Object> arrData) {
		super(context, resource, arrData);
		mContext = context;
		setArrObject(arrData);

	}

	class viewHolder {
		ImageView imgNewsModel;
		TextView txtNewsModelTitle;
		TextView txtNewsMOdelDescription;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return getArrObject().size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return getArrObject().get(position);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.adapter_news, parent, false);

		viewHolder holder = new viewHolder();
		holder.imgNewsModel = (ImageView) rowView
				.findViewById(R.id.imgNewsModel);
		holder.txtNewsModelTitle = (TextView) rowView
				.findViewById(R.id.txtNewsModelTitle);
		holder.txtNewsMOdelDescription = (TextView) rowView
				.findViewById(R.id.txtNewsModelDescription);

		NewsModuleDTO dto = (NewsModuleDTO) getArrObject().get(position);
		SettingsActivity st = new SettingsActivity();
		if (st.flag == 0) {
			holder.txtNewsModelTitle.setText(dto.getNewsModuleTitle());
			holder.txtNewsMOdelDescription.setText(dto
					.getNewsModuleDescription());
		} else if (st.flag == 1) {
			holder.txtNewsModelTitle.setText(dto.getNewsModuleTitleAr());
			holder.txtNewsMOdelDescription.setText(dto
					.getNewsModuleDescriptionAr());
		}

		String path = dto.getNewsModuleImage();
		String currentUrl = Constants.URL_IMAGE_PAHT + path;
		if(path != null){
			String[] strs = path.split("/");
			File f = new File(Environment.getExternalStorageDirectory() + "/"
					+ Constants.APP_NAME + "/" + strs[0] + "/" + strs[1]);
			if (f.exists()) {
				if(f.length() == 0){
					f.delete();
					PicassoUtils.getInstance().showImageFromUrl(mContext, currentUrl,
							R.drawable.bgday, holder.imgNewsModel);
					// TODO: create folder?

					PicassoUtils.getInstance().saveImageFromUrl(
							mContext.getApplicationContext(), currentUrl, strs[1],
							strs[0]);
				}else{
					Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
					holder.imgNewsModel.setImageBitmap(bmp);
				}
				
			} else {
				PicassoUtils.getInstance().showImageFromUrl(mContext, currentUrl,
						R.drawable.bgday, holder.imgNewsModel);
				// TODO: create folder?

				PicassoUtils.getInstance().saveImageFromUrl(
						mContext.getApplicationContext(), currentUrl, strs[1],
						strs[0]);
			}
		}
		

		return rowView;
	}

	public ArrayList<Object> getArrObject() {
		return arrObject;
	}

	public void setArrObject(ArrayList<Object> arrObject) {
		this.arrObject = arrObject;
	}

}